package com.tools;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import com.alibaba.fastjson.JSONObject;

import lombok.NoArgsConstructor;

public class GetHtmlContentUtils {
    private final static String PreUrl="https://fanyi.baidu.com/v2transapi?from=zh&to=en";

    public static String getTranslateResult(String keyWord) throws Exception {    //传入要搜索的单词
        URL url = new URL(PreUrl);            //生成完整的URL
        
        // 打开URL
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36");
        connection.setRequestProperty("cookie", "PSTM=1576545432; BIDUPSID=015FBF7B78F0DB6439414CE747A8AD83; BAIDUID=D23CB9F1BABB194BB59059BF3BE12A75:SL=0:NR=10:FG=1; H_PS_PSSID=1420_21121_30211_18560_30284_26350; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; FANYI_WORD_SWITCH=1; REALTIME_TRANS_SWITCH=1; SOUND_PREFER_SWITCH=1; APPGUIDE_8_2_2=1; from_lang_often=%5B%7B%22value%22%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%2C%7B%22value%22%3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%5D; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1575355956,1575455685,1576652154,1576653838; to_lang_often=%5B%7B%22value%22%3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%2C%7B%22value%22%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%5D; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1576655939; yjs_js_security_passport=a0e914256d671e00fc3c3f5e14912848bc54f757_1576655937_js; delPer=0; PSINO=6"); 
        
        // 设置是否向connection输出，因为这个是post请求，参数要放在
        // http正文内，因此�?要设为true
        connection.setDoOutput(true);
        // Read from the connection. Default is true.
        //connection.setDoInput(true);
        // 默认�? GET方式
        connection.setRequestMethod("POST");      
        // Post 请求不能使用缓存
        connection.setUseCaches(false);  
           //设置本次连接是否自动重定�? 
        connection.setInstanceFollowRedirects(true);    
        // 配置本次连接的Content-type，配置为application/x-www-form-urlencoded�?
        // 意�?�是正文是urlencoded编码过的form参数
        connection.setRequestProperty("content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        connection.connect();
        OutputStream  out = connection.getOutputStream();
        // 正文，正文内容其实跟get的URL�? '? '后的参数字符串一�?
        StringBuffer body =new StringBuffer();
        body.append("&").append("transtype=translang");
        body.append("&").append("simple_means_flag=3");
        body.append("&").append("sign="+Java4JS.excuteJs(keyWord));
        body.append("&").append("token=f3c1a1731b7a4178daccdcaf15f8a13b");
        body.append("&").append("query=" + URLEncoder.encode(keyWord, "UTF-8"));
        // DataOutputStream.writeBytes将字符串中的16位的unicode字符�?8位的字符形式写到流里�?
        out.write(body.toString().getBytes());
        //流用完记得关
        out.flush();
        out.close();
        // 得到输入流，即获得了网页的内�?
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        // 读取输入流的数据，并显示
        Trans content=new Trans();          //翻译结果
        while ((line = reader.readLine()) != null) {            //获取翻译结果的算�?
        	System.out.println(line);
        	content=JSONObject.parseObject(line,Trans.class);
        }
        return content.getTrans_result().getData()[0].getDst();//返回翻译结果
    }

    public static void main(String[] args) throws Exception {
		System.out.println(getTranslateResult("读取输入流的数据，并显示"));
	}
   
}
@lombok.Data
@NoArgsConstructor
class Trans{
	private TransResult trans_result;
	
}
@lombok.Data
class TransResult{
	private Data[] data;
}
@lombok.Data
class Data{
	private String dst;
}